package cn.com.zp.util.word;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

public class CreateWordUtil {

	public static void createDocContext(String file, WordModel model)
			throws DocumentException, IOException {
		// 设置纸张大小
		Document document = new Document(PageSize.A4);
		// 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
		RtfWriter2.getInstance(document, new FileOutputStream(file));
		document.open();
		// 设置中文字体
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 正文字体风格
		String aa = "通信管线建设征询表";
		Paragraph title = new Paragraph(aa, new Font(bfChinese, 16, Font.BOLD));
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFirstLineIndent(6);
		document.add(title);
		// 设置标题格式对齐方式
		title.setAlignment(Element.ALIGN_CENTER);
		Paragraph title1 = new Paragraph("\n编号：   " + model.getTitle(),
				new Font(bfChinese, 10, Font.BOLD));
		// 设置标题格式对齐方式
		title1.setAlignment(Element.ALIGN_RIGHT);
		document.add(title1);
		// 设置 Table 表格
		Table aTable = new Table(7);
		int width[] = { 50 / 4, 50 / 3, 50 / 4, 80 / 3, 30 / 3, 30 / 3, 60 / 3 };
		aTable.setWidths(width);// 设置每列所占比例
		aTable.setWidth(100); // 占页面宽度 90%
		aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
		aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
		aTable.setDefaultVerticalAlignment(Element.ALIGN_CENTER);
		aTable.setDefaultHorizontalAlignment(Element.ALIGN_CENTER);
		aTable.setAutoFillEmptyCells(true); // 自动填满
		aTable.setBorderWidth(0); // 边框宽度
		aTable.setBorderColor(new Color(0, 0, 0)); // 边框颜色
		aTable.setPadding(10);// 衬距，看效果就知道什么意思了
		aTable.setSpacing(0);// 即单元格之间的间距
		aTable.setBorder(0);// 边框
		// 设置表头

		/**
		 * cell.setHeader(true);是将该单元格作为表头信息显示； cell.setColspan(3);指定了该单元格占3列；
		 * 为表格添加表头信息时，要注意的是一旦表头信息添加完了之后， 必须调用 endHeaders()方法，否则当表格跨页后，表头信息不会再显示
		 */
		// 单位信息
		Font fontChinese = new Font(bfChinese, 12, Font.BOLD, Color.black);
		Cell cell = new Cell(new Phrase("申请建设\n单位", fontChinese));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setRowspan(4);
		aTable.addCell(cell);
		Cell cell01 = new Cell("名  称");
		aTable.addCell(cell01);
		cell01.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell01.setHorizontalAlignment(Element.ALIGN_CENTER);// ----------
		Cell cell2 = new Cell(model.getTitle());
		cell2.setColspan(5);
		aTable.addCell(cell2);

		Cell cell03 = new Cell("地  址");
		aTable.addCell(cell03);
		cell03.setVerticalAlignment(Element.ALIGN_CENTER);
		cell03.setHorizontalAlignment(Element.ALIGN_CENTER);
		Cell cell3 = new Cell(model.getTitle());
		cell3.setColspan(5);
		aTable.addCell(cell3);

		Cell cell04 = new Cell("联系人");
		cell04.setVerticalAlignment(Element.ALIGN_CENTER);
		cell04.setHorizontalAlignment(Element.ALIGN_CENTER);
		aTable.addCell(cell04);
		Cell cell4 = new Cell(model.getTitle());
		cell4.setColspan(2);
		aTable.addCell(cell4);

		Cell cell05 = new Cell("邮政编码");
		cell05.setVerticalAlignment(Element.ALIGN_CENTER);
		cell05.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell05.setColspan(2);
		aTable.addCell(cell05);
		aTable.addCell(new Cell(model.getTitle()));

		Cell cell06 = new Cell("电  话");
		cell06.setVerticalAlignment(Element.ALIGN_CENTER);
		cell06.setHorizontalAlignment(Element.ALIGN_CENTER);
		aTable.addCell(cell06);
		Cell cell6 = new Cell(model.getTitle());
		cell6.setColspan(2);
		aTable.addCell(cell6);

		Cell cell07 = new Cell("传  真");
		cell07.setVerticalAlignment(Element.ALIGN_CENTER);
		cell07.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell07.setColspan(2);
		aTable.addCell(cell07);
		Cell faxcell = new Cell(model.getTitle());
		faxcell.setColspan(1);
		aTable.addCell(faxcell);

		Font fontChinese1 = new Font(bfChinese, 12, Font.BOLD, Color.black);
		Cell cell22 = new Cell(new Phrase("申请建设\n路段", fontChinese1));
		cell22.setVerticalAlignment(Element.ALIGN_CENTER);
		cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell22.setBorderColor(new Color(0, 0, 0));
		// cell22.setRowspan(conroadobj.length+1);
		cell07.setColspan(2);
		aTable.addCell(cell22);

		// 添加路段信息
		Font fontChinese11 = new Font(bfChinese, 21 / 2, Font.NORMAL,
				Color.black);
		Cell cell08 = new Cell(new Phrase("序号", fontChinese11));
		aTable.addCell(cell08);
		cell08.setVerticalAlignment(Element.ALIGN_CENTER);
		cell08.setHorizontalAlignment(Element.ALIGN_CENTER);

		Cell cell09 = new Cell(new Phrase("路名", fontChinese11));
		aTable.addCell(cell09);
		cell09.setVerticalAlignment(Element.ALIGN_CENTER);
		cell09.setHorizontalAlignment(Element.ALIGN_CENTER);

		Cell cellroad = new Cell(new Phrase("路段", fontChinese11));
		aTable.addCell(cellroad);
		cellroad.setVerticalAlignment(Element.ALIGN_CENTER);
		cellroad.setHorizontalAlignment(Element.ALIGN_CENTER);

		Cell cell10 = new Cell("长度（m）");
		aTable.addCell(cell10);
		cell10.setVerticalAlignment(Element.ALIGN_CENTER);
		cell10.setHorizontalAlignment(Element.ALIGN_CENTER);

		Cell cell11 = new Cell("管孔数");
		aTable.addCell(cell11);
		cell11.setVerticalAlignment(Element.ALIGN_CENTER);
		cell11.setHorizontalAlignment(Element.ALIGN_CENTER);

		Cell cell12 = new Cell("建设方式\n（开挖或非开挖）");
		aTable.addCell(cell12);
		cell12.setVerticalAlignment(Element.ALIGN_CENTER);
		cell12.setHorizontalAlignment(Element.ALIGN_CENTER);

		// -----------
		Font fontChinese2 = new Font(bfChinese, 12, Font.BOLD, Color.black);
		Cell cell33 = new Cell(new Phrase("路径及管\n孔示意图", fontChinese2));
		cell33.setVerticalAlignment(Element.ALIGN_CENTER);
		cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell33.setBorderColor(new Color(0, 0, 0));
		cell33.setRowspan(1);
		aTable.addCell(cell33);

		Font fontChinese22 = new Font(bfChinese, 12, Font.BOLD, Color.black);
		Cell cell44 = new Cell(new Phrase("\n见 附 页\n", fontChinese22));
		cell44.setVerticalAlignment(Element.ALIGN_CENTER);
		cell44.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell44.setColspan(6);
		aTable.addCell(cell44);
		// --------------------------------------------
		Font fontChinese3 = new Font(bfChinese, 12, Font.BOLD, Color.black);
		Cell cell55 = new Cell(new Phrase("征询单位", fontChinese3));
		cell55.setVerticalAlignment(Element.ALIGN_CENTER);
		cell55.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell55.setBorderColor(new Color(0, 0, 0));
		aTable.addCell(cell55);

		Font fontChinese4 = new Font(bfChinese, 12, Font.NORMAL, Color.black);
		Cell cell66 = new Cell(new Phrase("\n信息化委员会（盖章）\n\n年   月   日\n",
				fontChinese4));

		cell66.setVerticalAlignment(Element.ALIGN_RIGHT);
		cell66.setHorizontalAlignment(Element.ALIGN_RIGHT);
		cell66.setColspan(6);
		aTable.addCell(cell66);

		document.add(aTable);
		// document.add(new Paragraph("\n"));

		Paragraph title2 = new Paragraph("信息化委员会制");
		// 设置标题格式对齐方式
		title2.setAlignment(Element.ALIGN_RIGHT);
		// title.setFont(titleFont);
		document.add(title2);
		/*
		 * //添加图片 Image img=Image.getInstance("d:\\logo.gif");
		 * img.setAbsolutePosition(0, 0);
		 * img.setAlignment(Image.RIGHT);//设置图片显示位置
		 * img.scaleAbsolute(12,35);//直接设定显示尺寸
		 * img.scalePercent(50);//表示显示的大小为原尺寸的50% img.scalePercent(25,
		 * 12);//图像高宽的显示比例 img.setRotation(30);//图像旋转一定角度 document.add(img);
		 */
		document.close();
		System.out.println("SUCCESS...");
	}

	// 批复文件
	@SuppressWarnings("deprecation")
	public static void createDocContext(String file, WordModel model, Date date)
			throws DocumentException, IOException {
		// 设置纸张大小
		Document document = new Document(PageSize.A4);
		RtfWriter2.getInstance(document, new FileOutputStream(file));
		document.open();
		// 设置中文字体
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 标题
		Paragraph titlepar = new Paragraph("   关于梅林路等路段管线施工的批复\n", new Font(
				bfChinese, 16, Font.BOLD));
		titlepar.setAlignment(Element.ALIGN_CENTER);
		titlepar.setFirstLineIndent(6);
		document.add(titlepar);
		Paragraph headpar = new Paragraph("信息管线有限公司：", new Font(bfChinese, 12,
				Font.NORMAL));
		document.add(headpar);
		StringBuffer head1String = new StringBuffer("      我委日前收到你公司关于在");

		head1String.append("路段进行管线施工的申请（").append(
				"）。经研究并征询其他运营商参建需求，原则同意你公司在以下路段进行管线施工。");
		Paragraph head1 = new Paragraph(head1String.toString(), new Font(
				bfChinese, 12, Font.NORMAL));
		head1.setSpacingAfter(2);
		head1.setSpacingBefore(2);
		document.add(head1);

		Paragraph headroad = new Paragraph(model.getTitle(), new Font(
				bfChinese, 12, Font.NORMAL));
		document.add(headroad);
		StringBuffer feetstring = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		feetstring.append("      本批复有效期为").append(sdf.format(date)).append("至");
		date.setMonth(date.getMonth() + 3);
		feetstring.append(sdf.format(date) + "。");
		feetstring.append("并根据有关规定办理建设施工相关手续。");
		Paragraph feetpar = new Paragraph(feetstring.toString(), new Font(
				bfChinese, 12, Font.NORMAL));
		document.add(feetpar);
		// 设置标题格式对齐方式
		document.close();
	}

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		CreateWordUtil cwu = new CreateWordUtil();
		WordModel model = new WordModel();
		model.setTitle("测试");
		try {
			cwu.createDocContext("c:\\test.doc", model);
			cwu.createDocContext("c:\\test1.doc", model, new Date());
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
